package cn.slipi.admin.handler;


import cn.slipi.admin.common.exception.base.BizException;
import cn.slipi.admin.entity.TSysPermission;
import cn.slipi.admin.entity.TSysPermissionModule;
import cn.slipi.admin.service.TSysPermissionService;
import cn.slipi.admin.service.dto.in.m.PermissionSaveDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InitPermission {
    private static final Logger LOG = LoggerFactory.getLogger(InitPermission.class);

    @Autowired
    private TSysPermissionService permissionService;

    @PostConstruct
    public void initPermission() throws IllegalAccessException, ClassNotFoundException {
        LOG.info("初始化权限开始");
        //获取权限点说明
        Map<String, String> permissionDescMap = new HashMap<>();
        Class permissionDescClazz = Class.forName("cn.slipi.admin.constants.permission.PermissionConstantsDesc");
        Field[] permissionDescList = permissionDescClazz.getFields();
        for (Field permissionDesc : permissionDescList) {
            String permissionKey = permissionDesc.getName();
            String permissionDescValue = permissionDesc.get(permissionKey).toString();
            permissionDescMap.put(permissionKey, permissionDescValue);
        }
        //获取权限点
        Class permissionClazz = Class.forName("cn.slipi.admin.constants.permission.PermissionConstants");
        Field[] permission = permissionClazz.getFields();
        List<TSysPermissionModule> permissionModules = this.permissionService.getPermissionModuleDropList();
        Integer moduleId = 0;
        if (permissionModules.size() > 0) {
            moduleId = permissionModules.get(0).getId();
        }
        for (Field field : permission) {
            String pKey = field.getName();
            String pValue = field.get(pKey).toString();
            String pDesc = permissionDescMap.getOrDefault(pKey, pKey);
            try {
                TSysPermission ePermission = this.permissionService.getPermissionByCode(pValue.toLowerCase());
                PermissionSaveDTO permissionSaveDTO = new PermissionSaveDTO();
                permissionSaveDTO.setName(pDesc);
                permissionSaveDTO.setAction(pValue);
                permissionSaveDTO.setDesc(pKey);
                if (ePermission == null) {
                    permissionSaveDTO.setCode(pValue.toLowerCase().replace('/', '.'));
                    permissionSaveDTO.setModuleId(moduleId);
                    this.permissionService.addPermission(permissionSaveDTO);
                    LOG.info("add-{}-{}", field.getName(), field.get(field.getName()));
                } else {
                    this.permissionService.updatePermission(ePermission.getId(), permissionSaveDTO);
                    LOG.info("update-{}-{}", field.getName(), field.get(field.getName()));
                }
            } catch (BizException biz) {
                LOG.warn("fail-{}-{}-{}", field.getName(), field.get(field.getName()), biz.getMsg());
            }
        }
        LOG.info("初始化权限完成");
    }

}
